<?php 
/**
 * 
 * @author tangbl
 *
 */
class PermissionModel extends Model {
	
	/**
	 * 根据条件数组查询节点
	 * @param  array $map 条件数组
	 * @return array 节点列表数组
	 */
	public function serach($id){
	
		$map  = array("id"=>$id);
		$json = sendRest("BDC/Node/find",$map);
		
		return $json['data'];
	}
	
	/**
	 * 列出应用节点
	 * 
	 */
	public function listNode($map=array()){
		
		$json = sendRest("BDC/Node/index",$map);
		return $json['data'];
	}
	
	/**
	 * 列出应用系统所有Module
	 */
	public function listModule($pid, $default='', $api='hdmin'){
		
		$map  = array("api"=>$api,"pid"=>$pid);
		$json = sendRest("BDC/Node/listModule",$map);
		$data = $json['data'];
		
		return $this->getSelectModule($data['all'], $data['exsits'], $default);
	}
	
	/**
	 * 列出应用系统所有action
	 */
	public function listAction($pid, $module, $default='', $api='hdmin'){
		
		$map  = array("api"=>$api,"pid"=>$pid,"module"=>$module);
		$json = sendRest("BDC/Node/listAction",$map);
		$data = $json['data'];
		
		return $this->getSelectAction($data['all'], $data['exsits'], $default);
	}
	
	/**
	 * 找出对应子节点方法
	 */
	public function listChild($pid, $module, $action='', $api='hdmin'){
		
		$map  = array("api"=>$api,"pid"=>$pid,"module"=>$module,"action"=>$action);
		$json = sendRest("BDC/Node/listAction",$map);
		$data = $json['data'];
		
		foreach($data['all'] as $v=>$val){
								
			if(in_array($v,$data['exsits'])){
				$row[] = array('flag'=>false,'name'=>$v);
			}else{
				$row[] = array('flag'=>true, 'name'=>$v);
			}
		}
		return $row;
	}
	
	/**
	 * 生成select Module
	 */
	public function getSelectModule($all, $exsits, $default=''){
		
		$html = '';
		foreach($all as $v){
			
			$select = $v == $default ? "selected" : "";
			
			if(in_array($v,$exsits)){
				$html .= '<option value="'.$v.'" '.$select.' disabled>'.$v.'</option>';
			}else{
				$html .= '<option value="'.$v.'" '.$select.'>'.$v.'</option>';
			}
		}
		return $html;
	}
	
	/**
	 * 生成select Action
	 */
	public function getSelectAction($all, $exsits, $default=''){
	
		$html = '';
		foreach($all as $v=>$val){
				
			$select = $v == $default ? "selected" : "";
				
			if(in_array($v,$exsits)){
				$html .= '<option value="'.$v.'" '.$select.' disabled>'.$v.'</option>';
			}else{
				$html .= '<option value="'.$v.'" '.$select.'>'.$v.'</option>';
			}
		}
		return $html;
	}
}